/**
 * 评星指令。实现五星评分
 */
APP.directive('commentBar', [function () {
  return {
    restrict: 'E',
    template: '<div class="col">' +
    '<i class="dhcfont font-size-l"  ng-class="{\'color-text-orange\':commentValue>=1,\'color-text-s\':commentValue<1,\'font-size-icon\':isBig}" ng-click="comment(0)" ng-bind="commentValue==1?\'&#xe673;\':\'&#xe672;\'"></i>' +
    '<i class="dhcfont font-size-l padding-left-5"  ng-class="{\'color-text-orange\':commentValue>=3,\'color-text-s\':commentValue<3,\'font-size-icon\':isBig}" ng-click="comment(1)" ng-bind="commentValue==3?\'&#xe673;\':\'&#xe672;\'"></i>' +
    '<i class="dhcfont font-size-l padding-left-5"  ng-class="{\'color-text-orange\':commentValue>=5,\'color-text-s\':commentValue<5,\'font-size-icon\':isBig}" ng-click="comment(2)" ng-bind="commentValue==5?\'&#xe673;\':\'&#xe672;\'"></i>' +
    '<i class="dhcfont font-size-l padding-left-5"  ng-class="{\'color-text-orange\':commentValue>=7,\'color-text-s\':commentValue<7,\'font-size-icon\':isBig}" ng-click="comment(3)" ng-bind="commentValue==7?\'&#xe673;\':\'&#xe672;\'"></i>' +
    '<i class="dhcfont font-size-l padding-left-5"  ng-class="{\'color-text-orange\':commentValue>=9,\'color-text-s\':commentValue<9,\'font-size-icon\':isBig}" ng-click="comment(4)" ng-bind="commentValue==9?\'&#xe673;\':\'&#xe672;\'"></i>' +
    '</div>',
    replace: true,
    scope: {
      commentValue: '=commentValue',//绑定到value属性上
      isHalf: '=isHalf',//半颗星 true或false
      isBig: '=isBig',//大小 big或small
      commentChange: '&commentChange'//外部方法
    },
    link: function (scope, element, attr) {
      //是否第二次选择该元素，半颗星用
      var isSecondSelected = false;
      //第二次选择该元素索引，半颗星用
      var isSecondSign = -1;
      /*初始化方法*/
      var init = function () {
        //scope.commentValue = 10;
        if (typeof(scope.isHalf) == 'undefined') {
          scope.isHalf = false;
        }
        if (typeof(scope.isBig) == 'undefined') {
          scope.isBig = false;
        }
      };
      init();
      /*星标点击事件*/
      scope.comment = function (index) {
        if (scope.isHalf) {//是否为半颗星
          isSecondSelected = index == isSecondSign;//需要判断是否是第二次点击同一元素
          if (isSecondSelected) {//第二次点击model值置为当前全颗星
            scope.commentValue = scope.commentValue + 1;
            isSecondSign = -1;
          } else {
            scope.commentValue = index * 2 + 1;//第一次点击model值置为半颗星
            isSecondSign = index;
          }
        } else {
          scope.commentValue = (index + 1) * 2;//全颗星算法
        }
        scope.commentChange();
      };
    }
  }
}]);
